-
Notifications
You must be signed in to change notification settings - Fork 0
/
Singly Linked List - Insert node to a specific position - based on user input.cpp
103 lines (96 loc) · 2.62 KB
/
Singly Linked List - Insert node to a specific position - based on user input.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
using namespace std;
struct NODE {
int data;
NODE *next;
};
NODE *head, *tail, *temp;
void add(int value){
if(head == NULL){
head = new NODE;
head->data = value;
head->next = 0;
tail = head;
} else {
tail->next = new NODE;
tail = tail->next;
tail->data = value;
tail->next = 0;
}
}
int addTo(int position, int value){
temp = head;
if(position >= 1){
for(int i = 1; i < position-1; i++){
temp = temp->next; // point to position immediate previous node
if(temp == 0){
break;
}
}
if(temp || position == 1){
NODE *newnode = new NODE;
newnode->data = value;
if(position == 1){
newnode->next = head;
head = temp = newnode;
if(head->next == 0) tail = head;
} else {
newnode->next = temp->next;
temp->next = newnode;
if(newnode->next == 0) tail = newnode;
}
}
}
if(position < 1 || temp == 0){
cout << "Data not inserted. Invalid position [" << position <<"]." << endl;
return false;
}
return true;
}
int print(NODE *ref){
if(ref == 0){
cout << "[EMPTY LIST]" << endl;
return false;
} else {
while(ref != 0){
cout << ref->data;
ref = ref->next;
if(ref != 0 ) cout << " => ";
}
cout << endl;
return true;
}
}
int main(){
cout << "Note! Enter zero (0) to exit from entering input. " << endl;
int input = 1, position = 0;
while(true){
if(head == 0){
cout << "Enter a value for the first/initial NODE: ";
cin >> input;
} else {
cout << "Enter a position to add a new NODE: ";
cin >> position;
cout << "Enter a value for the position [" << position << "]: ";
cin >> input;
}
if(input == 0){
cout << "Program exited!" << endl;
break;
} else {
bool added = true;
if(head == 0){
add(input);
} else {
added = addTo(position, input);
}
if(added){
position = (position) ? position : ++position;
cout << "Value [" << input << "] added at the position " << position << ". ";
cout << "Updated list: \t" << endl << "↳ ";
if(!print(head)) break;
}
cout << endl << endl;
}
}
}